﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace Fantacid303.ProcessAffinityControl.Manager.Extensions
{
	/// <summary>
	/// Extension method clas for <see cref=" ObservableCollection"/>
	/// </summary>
	public static class ObservableCollectionExtensions
	{
		/// <summary>
		/// Removes all occurences
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="src">The source.</param>
		/// <param name="match">The match.</param>
		public static void RemoveAll<T>(this ObservableCollection<T> src, Predicate<T> match )
		{
			for(var i=src.Count-1; i>=0 ; i--)
			{
				var item = src[i];
				if (match(item))
					src.RemoveAt(i);
			}
		}

		/// <summary>
		/// Iterates thorugh the collection and performs an action
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="src">The source.</param>
		/// <param name="action">The action.</param>
		public static void ForEach<T>( this ObservableCollection<T> src, Action<T> action )
		{
			foreach ( var item in src)
			{
				action(item);
			}
		}

		/// <summary>
		/// Adds multiple entries
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="src">The source.</param>
		/// <param name="items">The items.</param>
		public static void AddRange<T>( this ObservableCollection<T> src, IEnumerable<T> items)
		{
			foreach ( var item in items)
			{
				src.Add(item);
			}
		}
	}
}
